#include <nice2d/widgets/checkbox.h>
#include <nice2d/widgets/desktop.h>
#include <nice2d/background.h>
#include <nice2d/application.h>

IMPLEMENT_OBJECT(CheckBoxStyle, ControlStyle)

CheckBoxStyle::CheckBoxStyle()
{
    normalBackground = Background::create("./../data/theme/BabyBlue.png", true, {48, 40, 34, 36});
    uncheckedBackground = Background::create("./../data/theme/BabyBlue.png", true, {379, 2, 34, 36});
}

class CheckBoxData
{
public:
    bool checked = false;
};

IMPLEMENT_OBJECT(CheckBox, Control)

CheckBox::CheckBox(const sf::Vector2f &size):
    Control(size),
    data(new CheckBoxData)
{
    auto desktop = GET_COMPONENT(Application::getInstance(), Desktop);
    setControlStyle(desktop->getControlStyle("CheckBox"));

    auto style = getTemplateStyle<CheckBoxStyle>();
    setBackground(style->normalBackground);
}

CheckBox::~CheckBox()
{
}

void CheckBox::setChecked(bool value)
{
    if (data->checked != value) {
        data->checked = value;
        checked(value);
    }
}

bool CheckBox::isChecked() const
{
    return data->checked;
}

void CheckBox::onMouseEnter()
{
}

void CheckBox::onMouseExit()
{
}

void CheckBox::onMousePressed(sf::Mouse::Button button)
{
    auto style = getTemplateStyle<CheckBoxStyle>();

    if (isActive()) {
        if (isChecked()) {
            setBackground(style->uncheckedBackground);
            setChecked(false);
        } else {
            setBackground(style->normalBackground);
            setChecked(true);
        }
    }
}
